package gbench.appdemo.jdbcapp.gsx;

import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;
import gbench.common.tree.LittleTree.IRecord;
import static gbench.common.tree.LittleTree.IRecord.*;
import static gbench.common.tree.LittleTree.*;
import java.util.stream.*;
import org.junit.jupiter.api.Test;

public class JunitLmax {
    
    
   @Test
   public void foo() {
       
       final int buffSize = (int)Math.pow(2, 20);
       final  EventFactory<IRecord> factory = ()->REC();
       EventHandler<IRecord > handler = (event,sequence,endOfBatch)->{
           System.out.println(MFT("{0}:{1,number,#}--->{2},{3}",Thread.currentThread().getId(),sequence,event,endOfBatch));
       };
       final var disruptor = new Disruptor<IRecord>(factory, buffSize, DaemonThreadFactory.INSTANCE);
       disruptor.handleEventsWith(handler);
       disruptor.start();
       final var ringBuffer = disruptor.getRingBuffer();
       Stream.iterate(0,i->i<Math.pow(10, 6), i->i+1).parallel()
       .map(i->REC("name","name"+i)).forEach(rec->{
           ringBuffer.publishEvent((event,seq)->{// 发送事件
               event.compute("name", v->rec.get("name"));
               event.compute("seq",v->seq);
           });
       });
   };
   
   @Test
   public void bar() {
       for(int i=0;i<Math.pow(10, 6);i++) {
           System.out.println(i);
       }
   }

}
